package com.symaster.gameengine.e2d.renderer;

import com.symaster.gameengine.core.Renderer;
import org.lwjgl.opengl.GL11;

/**
 * 阴影渲染器
 *
 * @author yinmiao
 * @since 2024/9/20
 */
public class ShadowRenderer extends Renderer {

    private int shadowWidth, shadowHeight;

    public int getShadowWidth() {
        return shadowWidth;
    }

    public void setShadowWidth(int shadowWidth) {
        this.shadowWidth = shadowWidth;
    }

    public int getShadowHeight() {
        return shadowHeight;
    }

    public void setShadowHeight(int shadowHeight) {
        this.shadowHeight = shadowHeight;
    }

    /**
     * @return 当前渲染器需要框架分配的显示的大小
     */
    @Override
    public int getWidth() {
        return shadowWidth;
    }

    /**
     * @return 当前渲染器需要框架分配的显示的大小
     */
    @Override
    public int getHeight() {
        return shadowHeight;
    }

    /**
     * 子组件使用, 在当前Fbo缓冲区里面绘制
     */
    @Override
    protected void draw() {
        GL11.glColor4f(0 , 0, 0, 0.3f);

        // 绘制一个圆
        drawCircle(0.0f, 0.0f, 0.9f, 100);

        // 重置颜色设置
        GL11.glColor4f(1, 1, 1 , 1);
    }

    public void drawCircle(float cx, float cy, float r, int numSegments) {
        GL11.glBegin(GL11.GL_TRIANGLE_FAN);
        // GL11.glClearColor(1, 0, 0, 1);

        // 中心点
        GL11.glVertex2f(cx, cy);

        // 绘制圆上的各个顶点
        for (int i = 0; i <= numSegments; i++) {
            double theta = 2.0 * Math.PI * i / numSegments; // 当前角度
            float x = (float) (r * Math.cos(theta)); // 计算 x 坐标
            float y = (float) (r * Math.sin(theta)); // 计算 y 坐标

            GL11.glVertex2f(cx + x, cy + y); // 绘制顶点
        }

        GL11.glEnd();
    }
}
